package resources;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import resources.LanguageSelector.locales;

/**
 *
 * @author markel
 */
public class SelectLocaleServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
              throws IOException {
        locales locale = null;
        String localeName = req.getParameter("locale");
        if(localeName.equals(locales.en_UK.name()))
            locale = locales.en_UK;
        else if(localeName.equals(locales.es_ES.name()))
            locale = locales.es_ES;
        else if(localeName.equals(locales.eu_ES.name()))
            locale = locales.eu_ES;

        LanguageSelector.setLocale(locale);
        String[] url = req.getRequestURL().toString().split("selectLocale");

        String urlAppendix = req.getParameter("url");
        if(urlAppendix != null)
        {
        	//Se mira si la peticion a venido de product, porque si es asi hay que añadir el parametro ref
            String ref = req.getParameter("ref");
            if(ref != null)
            	resp.sendRedirect(url[0] + urlAppendix + "?ref=" + ref);
            else
            	resp.sendRedirect(url[0] + urlAppendix);
        }
        else
            resp.sendRedirect(url[0]);
    }

}